package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.XfNews;
import org.dromara.system.domain.query.XfNewsQuery;
import org.dromara.system.domain.vo.XfNewsVo;
import org.dromara.system.mapper.XfNewsMapper;
import org.dromara.system.service.IXfNewsService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfNewsServiceImpl extends ServiceImpl<XfNewsMapper, XfNews> implements IXfNewsService {

    @Override
    public TableDataInfo<XfNewsVo> selectPageXfNewsList(XfNewsQuery query) {
        Page<XfNews> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<XfNews> wrapper = Wrappers.lambdaQuery();

        // 根据新闻ID查询
        wrapper.eq(query.getNewsId() != null, XfNews::getNewsId, query.getNewsId());

        // 根据新闻标题模糊查询
        wrapper.like(StringUtils.isNotBlank(query.getNewsTitle()), XfNews::getNewsTitle, query.getNewsTitle());

        // 根据新闻类型查询
        wrapper.eq(StringUtils.isNotBlank(query.getNewsType()), XfNews::getNewsType, query.getNewsType());

        // 根据是否置顶查询
        wrapper.eq(StringUtils.isNotBlank(query.getIsTop()), XfNews::getIsTop, query.getIsTop());

        // 根据状态查询
        wrapper.eq(StringUtils.isNotBlank(query.getStatus()), XfNews::getStatus, query.getStatus());

        // 根据作者模糊查询
        wrapper.like(StringUtils.isNotBlank(query.getAuthor()), XfNews::getAuthor, query.getAuthor());

        // 根据新闻ID集合查询
        wrapper.in(query.getNewsIds() != null && !query.getNewsIds().isEmpty(), XfNews::getNewsId, query.getNewsIds());

        // 按创建时间降序排列
        wrapper.orderByDesc(XfNews::getCreateTime);

        Page<XfNewsVo> result = baseMapper.selectPageXfNewsList(page, wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public List<XfNewsVo> selectXfNewsList(XfNewsQuery query) {
        return baseMapper.queryList(query);
    }

    @Override
    public XfNewsVo selectXfNewsById(Long newsId) {
        return baseMapper.selectVoById(newsId);
    }

    @Override
    public boolean insertXfNews(XfNews news) {
        return this.save(news);
    }

    @Override
    public boolean updateXfNews(XfNews news) {
        return this.updateById(news);
    }

    @Override
    public int deleteXfNewsById(Long newsId) {
        return baseMapper.deleteById(newsId);
    }

    @Override
    public int deleteXfNewsByIds(Long[] newsIds) {
        return baseMapper.deleteBatchIds(List.of(newsIds));
    }
}
